import sys
from lattice_functions import *
from os import *
import matplotlib
from numpy import mean,std,sqrt
import pylab

# T-Student Function
def getTD(n):
    tD = {1:12.706,2:4.303,3:3.182,4:2.776,5:2.571,6:2.447,7:2.365,8:2.306,9:2.262,10:2.228,11:2.201,12:2.179,13:2.160,14:2.145,15:2.131,16:2.120,17:2.110,18:2.101,19:2.093,20:2.086,21:2.080,22:2.074,23:2.069,24:2.064,25:2.060,26:2.056,27:2.052,28:2.048,29:2.045,30:2.042 }
    tdBig = {40:2.021,60:2.000,120:1.980,1e5:1.960}
    if n in tD:
        return tD[n]
    else:
        for l,v in tdBig.iteritems():
            if n < l:
                return v
        return tdBig[1e5]


if len(sys.argv)<3: 
	exit()
original = sys.argv[1]
path = sys.argv[2]
topX = 100
orext,orlev,ormax = readLattice(original)
or100 = getTopX(orext,topX)

dirList = listdir(path)
dirList.sort()
data = {}
for fname in dirList:
	print path+"/"+fname
	extent,levels,maxlev = readLattice(path+"/"+fname)
	top100 = getTopX(extent,topX)
	s = 0
	n = 0
	amount = float(fname.replace(".lat","")[fname.rfind("-")+1:fname.rfind("_")])
	print amount	
	for t in top100:
		if t in or100:
			s += 1
		else:
			n += 1
	#print s,"/",n
	if amount not in data:
		data[amount] = []
	data[amount].append(s)

matplotlib.pyplot.figure()

x = data.keys()
y = []
x.sort()
yerror = []
for i in x:
	y.append(mean(data[i]))
	yerror.append(float(std(data[i])/sqrt(len(data[i])))*getTD(len(data[i])))
# Stability Distribution 
#h,bins=matplotlib.numpy.histogram(stabs, bins=10)
matplotlib.pyplot.errorbar(x,y,yerr=yerror,fmt='o',ecolor='green',mfc='green',mec='green',label='Toxic')
#matplotlib.pyplot.plot(x,y,'o')
matplotlib.pyplot.xlabel("Extent percentage")
matplotlib.pyplot.ylabel("Equal Stable Concepts")
pylab.show()
#readLattice(lattice)
